﻿using System;

namespace Demo0510
{
    class Program
    {
        static void Main(string[] args)
        {
            Triangular();
        }

        static void Square() {
            //            用户输入正方形边长，用* 打印出实心正方形。
            //            如：用户输入5，则输出如下图形。
            //*****
            //*****
            //*****
            //*****
            //*****
            //接收正方形边长
            Console.WriteLine("请输入正方形边长：");
            int a = int.Parse(Console.ReadLine());
            //定义二维数组
            string[,] arr = new string[a, a];
            //赋值
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    arr[i, j] = "*" + " ";
                }
            }
            //输出数组
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    Console.Write(arr[i, j]);
                }
                Console.WriteLine();
            }
        }
        static void MultiplicationTable() {
            //     1 * 1 = 1
            //2 * 1 = 2   2 * 2 = 4
            //3 * 1 = 3   3 * 2 = 6   3 * 3 = 9
            //4 * 1 = 4   4 * 2 = 8   4 * 3 = 12  4 * 4 = 16
            //5 * 1 = 5   5 * 2 = 10  5 * 3 = 15  5 * 4 = 20  5 * 5 = 25
            //6 * 1 = 6   6 * 2 = 12  6 * 3 = 18  6 * 4 = 24  6 * 5 = 30  6 * 6 = 36
            //7 * 1 = 7   7 * 2 = 14  7 * 3 = 21  7 * 4 = 28  7 * 5 = 35  7 * 6 = 42  7 * 7 = 49
            //8 * 1 = 8   8 * 2 = 16  8 * 3 = 24  8 * 4 = 32  8 * 5 = 40  8 * 6 = 48  8 * 7 = 56  8 * 8 = 64
            //9 * 1 = 9   9 * 2 = 18  9 * 3 = 27  9 * 4 = 36  9 * 5 = 45  9 * 6 = 54  9 * 7 = 63  9 * 8 = 72  9 * 9 = 81

            Console.WriteLine("打印九九成分表：");
            //定义数组
            string[,] arr = new string[9,9];
            //给数组赋值
            for (int i = 0; i <arr.GetLength(0); i++)
            {
                for (int j = 0; j <=i; j++)
                {
                    arr[i, j] = (i + 1) + "*" + (j + 1) + "=" + (i+1)*(j+1)+"\t";
                }
            }
            //输出二维数组
            for (int i = 0; i <arr.GetLength(0); i++)
            {
                for (int j = 0; j <arr.GetLength(1); j++)
                {
                    Console.Write(arr[i,j]);
                }
                Console.WriteLine();
            }
        }
        static void HollowSquare() {
            //        用户输入正方形边长，用* 打印出空心正方形。
            //如：用户输入5，则输出如下图形。
            //*****
            //*   *
            //*   *
            //*   *
            //*****

            //接收正方形边长
            Console.WriteLine("请输入正方形边长：");
            int a = int.Parse(Console.ReadLine());

            string[,] arr = new string[a ,a ];

            for (int i = 0; i <arr.GetLength(0); i++)
            {
                for (int j = 0; j <arr.GetLength(1) ; j++)
                {
                    arr[i, j] = "*";
                }
            }

            for (int i = 0; i <arr.GetLength(0); i++)
            {
                for (int j = 0; j <arr.GetLength(1); j++)
                {
                    if (j==0||i==0|| j == arr.GetLength(0) - 1 || i==arr.GetLength(1)-1 )
                    {
                        Console.Write(arr[i, j]);
                    }
                    else {
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }

            Console.ReadKey();

        }
        static void Rhombus() {

//            *
//           ***
//          *****
//           ***
//            *

//          边长是3
//行列数就是5

            Console.WriteLine("请输入菱形边长：");
            int a = int.Parse(Console.ReadLine());

            string[,] arr = new string[a, a];
          

            for (int i = 1; i <=(arr.GetLength(0)+1)/2; i++)//打印上半部分的菱形
            {
                for (int j = (arr.GetLength(1) - 1)/2; j >=i; j--)
                {
                    Console.Write(" ");//打印空格
                }
                for (int k = 1;  k <=i*2-1;  k++)
                {//打印“*”号，第i行有i*2-1个“*”号
                    Console.Write("*");
                }
                Console.WriteLine();
            }//上面的

            for (int i = (arr.GetLength(1) - 1) / 2; i >=1; i--)
            {
                for (int j = i-1; j <(arr.GetLength(1)-1)/2; j++)
                {
                    Console.Write(" ");
                }
                for (int k = 1; k <=i*2-1; k++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }

            Console.ReadKey();
        }
        static void Subscript() {
            //在 Main 方法中创建一个字符串类型的数组，
            //并存入 5 个值，然后将数组中下标是偶数的元素输出。
            string[] arr = { "56", "45", "123", "87", "96" };
            int indext = -1;
            for (int i = 0; i <arr.GetLength(0); i++)
            {
                indext = i;
                if (indext%2!=0)
                {
                    Console.Write(arr[i]+" ");
                }
            }

        }
        static void Sort1() {
            //如果一个数组保存元素是有序的（从大到小），
            //向这个数组中插入一个数，使得插入后的数组元素仍然保持有序。
            int [] arr = {54,8,44,98,155};
            int[] arr1 = new int[6];
            int temp = 0;
            //Array.Sort(arr);//从小到大
            Array.Reverse(arr);//从大到小
            Console.WriteLine("插入前数组：");
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                Console.Write(arr[i]+" ");
            }
            Console.WriteLine();

            Console.WriteLine("请输入要插入的数字：");
            arr1[5] = int.Parse(Console.ReadLine());
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                arr1[i] = arr[i];
            }
            Console.WriteLine("插入数字后的数组：");
            foreach (int item in arr1)
            {
                Console.Write(item+" ");
            }
            for (int i = 0; i < arr1.GetLength(0)-1; i++)
            {
                for (int j = 0; j <arr1.GetLength(0)-1-i; j++)
                {
                    if (arr1[j]<arr1[j+1]) {
                        temp = arr1[j];
                        arr1[j] = arr1[j + 1];
                        arr1[j + 1] = temp;
                    }
                }
            }
        }
        static void Triangular() {
            Console.WriteLine("请输入长度：");
            int length = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine();
            int[,] array = new int[length, length];
            for (int i = 0; i < length; i++)
            { // 循环打印杨辉三角,length行
                for (int k = 0; k < length - i; k++) //打印空格
                {
                    Console.Write(" ");
                }
                for (int j = 0; j <= i; j++) //注意:j<=i, 因为第1行有1列，第2行有2列，第3行有3列。。。
                {
                    if (j == 0 || i == j)  //第一列和最后一列
                    {
                        array[i, j] = 1; //值为1
                    }
                    else
                    {
                        array[i, j] = array[i - 1, j - 1] + array[i - 1, j]; //中间列的值 = 上一行和它所在列-1的值 + 上一行和它所在列的值
                    }
                    Console.Write(array[i, j].ToString() + " "); //打印值
                }
                Console.WriteLine();//每行打印完所有值后换行
            }
            Console.ReadKey();
        }
    }
}
